package com.alibaba.datax.core.transport.transformer;

import com.alibaba.datax.common.element.Column;
import com.alibaba.datax.common.element.Record;
import com.alibaba.datax.common.element.StringColumn;
import com.alibaba.datax.common.exception.DataXException;
import com.alibaba.datax.transformer.Transformer;

import java.util.Arrays;

/**
 * @Description
 * @Author zhangsong
 * @Date 2024/4/3
 **/
public class MysqlTransFormerTest extends Transformer {

    public MysqlTransFormerTest(){
        setTransformerName("dx_test_transfomer");
    }
    //这里的record就是读入的每一条数据
    //paras是传入的参数，就是json配置中的parameter参数
    @Override
    public Record evaluate(Record record, Object... paras) {
        int columnIndex;
        try {
            if (paras.length != 1) {
                throw new RuntimeException("dx_substr paras must be 1");
            }
            columnIndex = (Integer) paras[0];
        } catch (Exception e) {
            throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER, "paras:" + Arrays.asList(paras).toString() + " => " + e.getMessage());
        }
        Column column = record.getColumn(columnIndex);
        //有几列
        System.out.println(record.getColumnNumber());
//        //指定列的数据（占用大小，数据内容，数据类型）--{"byteSize":2,"rawData":"aa","type":5}
//        System.out.println(record.getColumn(0));
        try {
            String oriValue = column.asString();
            //如果字段为空，跳过subStr处理
            if (oriValue == null) {
                return record;
            }
            String newValue=oriValue+"--Append";
            record.setColumn(columnIndex, new StringColumn(newValue));
        }catch (Exception e) {
            throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_RUN_EXCEPTION, e.getMessage(),e);
        }
        return record;
    }
}
